<template>
  <!-- 项目巡查 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="100px" label="请选择工地" prop="constructionSiteId">
        <el-select v-model="queryParams.constructionSiteId" clearable  placeholder="请选择工地">
          <el-option
            v-for="(v,index) in constructionSiteList"
            :key="index"
            :label="v.name"
            :value="v.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['violation:check:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['violation:check:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['violation:check:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['violation:check:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border fit highlight-current-row empty-text="暂无数据" v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <el-table-column label="工地名称" align="center" prop="constructionSite" />
<!--      <el-table-column label="图片路径，逗号隔开" align="center" prop="imgUrls" />-->
<!--      <el-table-column label="视频路径，逗号隔开" align="center" prop="videoUrls" />-->
      <el-table-column label="是否完工" align="center" prop="isProjectCompleted">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isfinishwork" :value="scope.row.isProjectCompleted"/>
        </template>
      </el-table-column>
      <el-table-column label="是否重点监管" align="center" prop="isImportFocus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iskeycharge" :value="scope.row.isImportFocus"/>
        </template>
      </el-table-column>
      <el-table-column label="巡查说明" align="center" prop="content" />
      <!-- <el-table-column label="巡查人员ID" align="center" prop="userId" /> -->
      <!-- <el-table-column label="数据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_show" :value="scope.row.status"/>
        </template>
      </el-table-column> -->
      <el-table-column label="巡查时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="项目施工阶段ID" align="center" prop="constructionPhaseId" /> -->
      <!-- <el-table-column label="是否显示" align="center" prop="isShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_show" :value="scope.row.isShow"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['violation:check:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDesc(scope.row)"
            v-hasPermi="['modules:company:query']"
          >详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['violation:check:remove']"
          >删除</el-button> -->

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目巡查记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="fillUpForm" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="施工工地" prop="constructionSiteId">
              <el-select :disabled="title == descTitle" v-model="form.constructionSiteId" clearable  :placeholder="title == descTitl?'':'工地名称'" @change="constructionSiteChange">
                <el-option
                  v-for="(v,index) in constructionSiteList"
                  :key="index"
                  :label="v.name"
                  :value="v.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="项目是否完工" prop="isProjectCompleted">
              <el-select :disabled="title == descTitle" v-model="form.isProjectCompleted" clearable  placeholder="请选择">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否重点监管" prop="isImportFocus">
              <el-select :disabled="title == descTitle" v-model="form.isImportFocus" clearable  placeholder="请选择">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
            <el-col :span="8">
              <el-form-item label="执法人员姓名" prop="userId">
                <el-input :disabled="title == descTitle" v-model="form.lawUserName" :placeholder="title == descTitl?'':'请输入巡查人员ID'" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="巡查时间" prop="checkTime">
                <el-date-picker :disabled="title == descTitle" clearable
                  v-model="form.checkTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="title == descTitl?'':'请选择巡查时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            
            <!-- <el-col :span="8">
              <el-form-item label="是否显示" prop="isShow">
                <el-select :disabled="title == descTitle" v-model="queryParams.isRead" clearable placeholder="请选择">
                  <el-option label="显示" :value="1"></el-option>
                  <el-option label="不显示" :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
        </el-row>
              
        <el-row>
          <!-- <el-col :span="8">
            <el-form-item label="图片路径，逗号隔开" prop="imgUrls">
              <el-input type="textarea" v-model="form.imgUrls" placeholder="请输入图片路径，逗号隔开" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="视频路径，逗号隔开" prop="videoUrls">
              <el-input type="textarea" v-model="form.videoUrls" placeholder="请输入视频路径，逗号隔开" />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="巡查说明备注" prop="content">
              <el-input :disabled="title == descTitle" v-model="form.content" type="textarea" :placeholder="title == descTitl?'':'请输入巡查说明备注'" />
              <!-- <el-input v-model="form.isShow" placeholder="请输入是否显示，0不显示，1显示" />-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="现场图片" prop="imageUrl">
              <image-upload v-if="title != descTitle" v-model="form.imgUrls"/>
              <div v-if="title == descTitle">
                <div style="display: inline-block; margin: 0 8px 8px 0; border: 1px solid #c0ccda; border-radius: 6px;" v-for="(item, i) in form.imgUrls" :key="i">
                  <el-image
                    @click="handlePictureCardPreview(item)"
                    style="width: 150px; height: 150px"
                    :src="item.url"
                    fit="cover"></el-image>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="现场视频" prop="videoUrls">
              <file-upload v-if="title != descTitle" v-model="form.videoUrls"/>
              <el-video v-if="form.videoUrls && title != descTitle"
                        :src="form.videoUrls"
                        :autoplay="false"
                        :loop="false"
                        :controls="true"
                        width="640"
                        height="360"
              ></el-video>
              
              <video v-if="title == descTitle && form.videoUrls" width="320" height="240" controls>
                <source :src="form.videoUrls" type="video/mp4" />
              </video>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-form-item label="巡查说明备注">
            <editor :readOnly="title == descTitle" v-model="form.content" :min-height="192"/>
          </el-form-item>
        </el-row> -->
      </el-form>
      <div  slot="footer" class="dialog-footer">
        <el-button v-if="title != descTitle" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck,pageCheck } from "@/api/violation/check";

import { listSite } from "@/api/construction/site";

export default {
  name: "Check",
  dicts: ['iskeycharge', 'istop', 'is_show', 'isfinishwork'],
  data() {
    return {
      //工地信息
      projectList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目巡查记录表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        constructionSiteId: null,
        imgUrls: null,
        videoUrls: null,
        isProjectCompleted: null,
        isImportFocus: null,
        content: null,
        userId: null,
        status: null,
        checkTime: null,
        constructionPhaseId: null,
        isShow: null,
        reportNumber:null,
        lat:null,
        lon:null,
        areaAddress:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // constructionSiteId: [
        //   { required: true, message: "工地ID不能为空", trigger: "blur" }
        // ],
        constructionSiteId: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        isProjectCompleted: [
          { required: true, message: "请选择项目是否完工", trigger: "change" }
        ],
        isImportFocus: [
          { required: true, message: "请选择是否重点监管", trigger: "change" }
        ],
      },
      constructionSiteList: [],
      descTitle: '查看项目巡查记录', // 详情标题

      
      dialogImageUrl: '',
      dialogVisible: false,
    };
  },
  created() {
    this.getList();

    
    this.initConstructionSiteList(); // 查询工地
  },
  methods: {
    /** 查询项目巡查记录列表 */
    getList() {
      this.loading = true;
      pageCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        constructionSiteId: null,
        imgUrls: null,
        videoUrls: null,
        isProjectCompleted: null,
        isImportFocus: null,
        content: null,
        userId: null,
        status: null,
        createTime: null,
        updateTime: null,
        checkTime: null,
        constructionPhaseId: null,
        isShow: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目巡查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目巡查记录";
      });
    },
    /** 查询按钮操作 */
    handleDesc(row) {
      this.reset();
      const id = row.id || this.ids
      getCheck(id).then(response => {
        this.form = response.data;

        let baseUploadUrl = process.env.VUE_APP_BASE_API
        // let baseUploadUrl = 'http://192.168.31.156:8808/'
        let getLast = baseUploadUrl[baseUploadUrl.length-1]
        console.log(process.env.VUE_APP_BASE_API)
        if(getLast == '/'){
          baseUploadUrl = baseUploadUrl.slice(0, -1)
        }
        console.log(baseUploadUrl, 'baseUploadUrl')
        
        
        // 处理图片
        if(this.form.imgUrls){
           // 首先将值转为数组
           let list = Array.isArray(this.form.imgUrls) ? this.form.imgUrls : this.form.imgUrls.split(',');
          // 然后将数组转为对象数组
          let imgList = []
          if(list.length>0){
            
            imgList = list.map(item => {
              if (typeof item === "string") {
                if (item.indexOf('http') === -1) {
                    item = { name: item, url: baseUploadUrl + item };
                } else {
                    item = { name: item, url: item };
                }
              }
              return item;
            });
          }
          this.form.imgUrls = imgList
        }

        // 处理视频链接
        if(this.form.videoUrls){
          this.form.videoUrls = baseUploadUrl + this.form.videoUrls
        }

        this.open = true;
        this.title = this.descTitle;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目巡查记录编号为"' + ids + '"的数据项？').then(function() {
        return delCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('violation/check/export', {
        ...this.queryParams
      }, `check_${new Date().getTime()}.xlsx`)
    },
    
    // 工地名称
    constructionSiteChange(v){
      
      var obj = {};
      obj = this.projectList.find(function(item) {
        return item.id === v;
      });
      console.log("site");
      console.log(obj);
      this.form.constructionSiteName = obj.name;
      this.form.constructionSiteId = v;

    },
    
    initConstructionSiteList() {
      listSite().then(response => {
        this.constructionSiteList = response.data;
      });
    },

    
    // 预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },


  }
};
</script>

<style lang="scss" scoped>
  
  .fillUpForm {
    .el-input{
      width: 100%;
    }
    .el-select{
      width: 100%;
    }
  }

</style>